package com.thank.concurrency.samples.singleton;

import com.thank.concurrency.annoations.Recommend;
import com.thank.concurrency.annoations.ThreadSafe;

/**
 * 枚举模式: 使用枚举方式的单例模式, 最安全
 */
@ThreadSafe
@Recommend
public class SingletonExample7 {

    // 私有的构造方法
    private SingletonExample7() {
    }

    public static SingletonExample7 getInstance() {
        return Singleton.INSTANCE.getInstance();
    }

    private enum Singleton {
        INSTANCE;

        private SingletonExample7 singleton;

        // JVM保证该方法绝对只被调用一次, 所以保证了线程安全
        Singleton () {
            singleton = new SingletonExample7();
        }

        public SingletonExample7 getInstance() {
            return singleton;
        }
    }

}
